﻿// -----------------------------------------------------------------------
// <copyright file="PushNotificationHandler.cs" company="Zen Design Corp">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Pap.Web
{
	using System.IO;
	using System.Text;
	using System.Web;
	using Microsoft.Practices.ServiceLocation;
	using Zen.BlackBerry.Pap.Control;
	using Zen.BlackBerry.Pap.Parser;

	/// <summary>
	/// Implements an HTTP handler for processing push notifications sent from
	/// the Push Proxy Gateway.
	/// </summary>
	/// <remarks>
	/// The request is parsed and handed off to the registered implementation
	/// of <see cref="IPapService"/> for further processing and the response
	/// from the call is packaged as an HTTP response to conclude processing.
	/// </remarks>
	public class PushNotificationHandler : IHttpHandler
	{
		#region IHttpHandler Members

		/// <summary>
		/// Gets a value indicating whether another request can use the
		/// <see cref="T:System.Web.IHttpHandler"/> instance.
		/// </summary>
		/// <value></value>
		/// <returns>
		/// true if the <see cref="T:System.Web.IHttpHandler"/> instance is
		/// reusable; otherwise, false.
		/// </returns>
		public bool IsReusable
		{
			get
			{
				return true;
			}
		}

		/// <summary>
		/// Enables processing of HTTP Web requests by a custom HttpHandler
		/// that implements the <see cref="T:System.Web.IHttpHandler"/>
		/// interface.
		/// </summary>
		/// <param name="context">
		/// An <see cref="T:System.Web.HttpContext"/> object that provides
		/// references to the intrinsic server objects (for example, Request,
		/// Response, Session, and Server) used to service HTTP requests.
		/// </param>
		public void ProcessRequest(HttpContext context)
		{
			// TODO: Allow PAP service to decode the notification message
			PapMessage message = PapMessageParser.Read(context.Request.InputStream);
			ResultNotification notification = message as ResultNotification;
			if (notification != null)
			{
				// Pass notification to PAP service for processing
				IPapService papService = ServiceLocator.Current.GetInstance<IPapService>();
				ResultNotificationResponseControl response = papService.ReceiveNotification(notification, context.Request.Params);
				PapRequestMessage responseMessage = new PapRequestMessage(response);

				// Stream response to caller
				context.Response.ContentType = responseMessage.ContentTypeForHeader;
				context.Response.ContentEncoding = Encoding.UTF8;
				context.Response.StatusCode = 200;
				using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))
				{
					responseMessage.WriteTo(writer);
				}

				context.Response.End();
			}
			else
			{
				context.Response.StatusCode = 400; // Bad request
				context.Response.StatusDescription = "Expected result notification message.";
				context.Response.End();
			}
		}

		#endregion
	}
}
